/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.demo;

import java.util.List;
import org.junit.Test;
import org.osdtsystem.exceptions.EdgeNotFoundException;
import org.osdtsystem.exceptions.GraphException;
import org.osdtsystem.exceptions.IllegalOrderException;
import org.osdtsystem.exceptions.IllegalRelationException;
import org.osdtsystem.exceptions.NodeNotFoundException;
import org.osdtsystem.exceptions.NonUniqueNameException;
import org.osdtsystem.graphs.Graph;
import org.osdtsystem.graphs.GraphEngine;
import org.osdtsystem.graphs.views.DependencyAlignment;
import org.osdtsystem.graphs.views.DependencyAlignmentEngine;
import org.osdtsystem.graphs.views.DependencyTree;
import org.osdtsystem.graphs.views.DependencyTreeEngine;
import org.osdtsystem.graphs.views.NodeCollection;
import org.osdtsystem.graphs.views.NodeCollectionEngine;
import org.osdtsystem.graphs.views.WordAlignment;
import org.osdtsystem.graphs.views.WordAlignmentEngine;
import org.osdtsystem.values.EdgeID;
import org.osdtsystem.values.Feature;
import org.osdtsystem.values.NodeID;
import org.osdtsystem.values.StringFeature;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class ViewDemo {
    @Test
    public void testSetup() throws GraphException {
        System.out.println("DependencyTree1");
        dependencyTree1();

        System.out.println("DependencyAlignmentDemo");
        dependencyAlignmentDemo();
    }

    public Graph dependencyTree1() throws GraphException {
        // Create a new graph for storing the data
        GraphEngine graph = new GraphEngine();

        // Create node collection and top node
        NodeCollection nodes = new NodeCollectionEngine(graph, "nodes");
        NodeID top = nodes.addNode();

        // Create new unordered dependency tree
        DependencyTree dependencyTree = new DependencyTreeEngine(graph, 
                nodes, top, "dependencies");

        // Create source dependency tree "the X of Y"
        NodeID nodeThe = dependencyTree.addNode(top, new StringFeature("root"));
        NodeID nodeX = dependencyTree.addNode(nodeThe, new StringFeature("nobj"));
        NodeID nodeOf = dependencyTree.addNode(nodeThe, new StringFeature("mod"));
        NodeID nodeY = dependencyTree.addNode(nodeOf, new StringFeature("nobj"));

        // Set node features
        Feature wordFeature = new StringFeature("word");
        dependencyTree.addNodeFeature(wordFeature);
        graph.setFeatureValue(nodeThe, wordFeature, "the");
        graph.setFeatureValue(nodeX, wordFeature, "X");
        graph.setFeatureValue(nodeOf, wordFeature, "of");
        graph.setFeatureValue(nodeY, wordFeature, "Y");

        // Set node names
        graph.setNodeName(nodeThe, "the");
        graph.setNodeName(nodeX, "X");
        graph.setNodeName(nodeOf, "of");
        graph.setNodeName(nodeY, "Y");

        // Find governor edge and governor
        EdgeID edgeThe = dependencyTree.governorEdge(nodeThe);
        NodeID governorThe = dependencyTree.governor(edgeThe);
        List<EdgeID> childrenThe = dependencyTree.dependentEdges(nodeThe);
        NodeID firstChildThe = dependencyTree.dependent(childrenThe.get(0));

        // Print graph
        System.out.println("===tree1===");
        System.out.println(graph.toOSDT());
        System.out.println("===========");
        // Return graph
        return graph;
    }
        
    
    public Graph dependencyAlignmentDemo() 
            throws NodeNotFoundException, IllegalRelationException, NonUniqueNameException, IllegalOrderException {
        // Create a new graph for storing the data
        Graph graph = new GraphEngine();

        // Create source and target node collections, and source and target top nodes
        NodeCollection sourceNodes = new NodeCollectionEngine(graph, "source nodes");
        NodeCollection targetNodes = new NodeCollectionEngine(graph, "target nodes");
        NodeID sourceTop = sourceNodes.addNode();
        NodeID targetTop = targetNodes.addNode();

        // Create new source and target dependency trees
        DependencyTree sourceTree = new DependencyTreeEngine(graph, 
                sourceNodes, sourceTop, "source tree");
        DependencyTree targetTree = new DependencyTreeEngine(graph, 
                sourceNodes, sourceTop, "target tree");

        // Create source dependency tree "the X of Y"
        NodeID snodeThe = sourceTree.addNode(sourceTop, new StringFeature("root"));
        NodeID snodeX = sourceTree.addNode(snodeThe, new StringFeature("nobj"));
        NodeID snodeOf = sourceTree.addNode(snodeThe, new StringFeature("mod"));
        NodeID snodeY = sourceTree.addNode(snodeOf, new StringFeature("nobj"));
        
        // Create target dependency tree "X 's Y"
        NodeID tnodeS = targetTree.addNode(targetTop, new StringFeature("root"));
        NodeID tnodeX = targetTree.addNode(tnodeS, new StringFeature("possr"));
        NodeID tnodeY = targetTree.addNode(tnodeS, new StringFeature("possd"));
        
        // Create new word alignment
        WordAlignment wordAlignment = new WordAlignmentEngine(graph, sourceNodes, targetNodes, "");

        // Link nodes in word alignment (the+of='s, X=X, Y=Y)
        wordAlignment.add(snodeX, tnodeX);
        wordAlignment.add(snodeY, tnodeY);
        wordAlignment.add(snodeThe, snodeOf, tnodeS);
        
        // Create new dependency alignment
        DependencyAlignment dependencyAlignment = 
                new DependencyAlignmentEngine(graph, sourceTree, targetTree, 
                    wordAlignment, "dependency alignment");
        
        // Recompute dependency alignment
        dependencyAlignment.recompute();
        
        // Print all dependency alignments (each alignment is represented by a 
        // single alignment node)
        for (NodeID alignment : dependencyAlignment.alignments()) {
            System.out.print("alignment " + alignment + ":");
            for (NodeID node : dependencyAlignment.nodes(alignment)) {
                System.out.print(node + " ");
            }
            System.out.println("\n");
        }
        
        // Return graph
        return graph;
    }
}
